<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 회원관리 컨트롤러
 */
class Member extends CI_Controller {
	
	var $data = array();
	
	function __construct() {
		parent::__construct();

		$this->load->model('member/Member_model', 'model', TRUE);
	}

	// Default
	function index() {
		$this->member_list();
	}

	/**
	 * 회원 리스트
	 */
	function member_list() {
		log_message('debug', "@@@@ Member::member_list()");

		$user_id      = $this->input->get('user_id', TRUE);
		$user_name    = $this->input->get('user_name', TRUE);
		$nickname     = $this->input->get('nickname', TRUE);
		$start_dt     = $this->input->get('start_dt', TRUE);
		$end_dt       = $this->input->get('end_dt', TRUE);
		$page         = $this->input->get('page', TRUE);

		$user_id      = isset($user_id) ? $user_id : '';
		$user_name    = isset($user_name) ? $user_name : '';
		$nickname     = isset($nickname) ? $nickname : '';
		$start_dt     = isset($start_dt) ? $start_dt : '';
		$end_dt       = isset($end_dt) ? $end_dt : '';
		$current_page = empty($page) ? 1 : $page;

		$page_size  = LIST_PAGE_SIZE;		// 한 페이지에 보여줄 글 수
		$block_size = LIST_BLOCK_SIZE;	// 네비영역의 한 블록에 보여줄 페이지 수

		$total_cnt  = $this->model->get_member_total_count($user_id, $user_name, $nickname, $start_dt, $end_dt);	// 총 글 수

		$this->data['list'] = $this->model->select_member_list($user_id, $user_name, $nickname, $start_dt, $end_dt, $current_page, $page_size);
		$this->data['total_cnt'] = $total_cnt;
		
		foreach ($this->data['list'] as &$item) {
			$item['ticket'] = $this->model->select_ticket_list($item['user_id'], '');
		}


		$this->load->helper(array('pagenavi'));
		$this->data['navi'] = page_navigation($current_page, $total_cnt, $page_size, $block_size);
		
		$this->load->view('member/member_list_v', $this->data);
	}

	/**
	 * 유료 회원 리스트
	 */
	function vip_member_list() {
		log_message('debug', "@@@@ Member::vip_member_list()");

		$this->data['member_list'] = $this->model->select_member_vip_list();
		
		$this->load->view('member/vip_member_list_v', $this->data);
	}
	
	/**
	 * 회원 서비스 사용 내역
	 */
	function member_history() {
		log_message('debug', "@@@@ Member::member_history()");
		
		$user_id = $this->input->get('user_id', TRUE);
		
		$this->load->model('member/History_model', 'history', TRUE);
		$this->data['service_list'] = $this->history->select_service_history_list($user_id, 1, 100);
		$this->data['payment_list'] = $this->history->select_payment_history_list($user_id, 1, 100);
		$this->data['point_list'] = $this->history->select_point_history_list($user_id, 1, 100);
		
		$this->load->view('member/member_history_pop_v', $this->data);
	}
	
	/**
	 * 상품 업그레이드 폼
	 */
	function change_package() {
		log_message('debug', "@@@@ Member::change_package()");
		
		// 현재 가입상품 정보
		$user_id = $this->input->get('user_id', TRUE);
		$this->data['member_package'] = $this->model->get_member_package_with_coupon_list($user_id);
		
		$this->load->view('member/change_package_pop_v', $this->data);
	}

	/**
	 * 상품 업그레이드 처리
	 */
	function change_package_ok() {
		log_message('debug', "@@@@ Member::change_package_ok()");
		
		$target_id        = $this->input->post('user_id', TRUE);
		$idx              = $this->input->post('idx', TRUE);
		$service_group_id = $this->input->post('service_group_id', TRUE);
		$qty              = $this->input->post('qty', TRUE);
		
		$result = $this->model->upgrade_member_package($idx);
		
		if ($result == TRUE) {
			$this->load->model('admin/History_model', 'history', TRUE);
			$this->history->insert_admin_history($this->data['sess_userinfo']['user_id'], $target_id, 'member_package', 'VIP회원권 업그레이드 (실버->골드)', '이벤트');
			
			echo "<script type='text/javascript'>";
			echo "	alert('업그레이드가 완료되었습니다.');";
			echo "	opener.location.reload();";
			echo "	window.close();";
			echo "</script>";
		} else {
			echo "<script type='text/javascript'>";
			echo "	alert('업그레이드를 실패하였습니다.');";
			echo "	history.back();";
			echo "</script>";
		}
	}
}

/* End of file member.php */
/* Location: ./application/controllers/member/member.php */